package com.yellowpage.sample.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.sample.data.HtmlTemplate;
import com.yellowpage.sample.data.News;
import com.yellowpage.sample.service.HtmlTemplateService;
import com.yellowpage.sample.service.NewsService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class NewsAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	private List<News> newsList = new ArrayList<News>();
	private List<Map> resultList = new ArrayList<Map>();

	private News news;
	private NewsService newsService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	
	private String query_htmlTemplateId;
	private String query_pubTime;
	private String query_title;
	
	private HtmlTemplateService htmlTemplateService;
	private List<HtmlTemplate> htmlTemplateList = new ArrayList<HtmlTemplate>();
	

	public void prepare() {
		super.prepare();
		
		htmlTemplateList = htmlTemplateService.getAllHtmlTemplate();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.news = newsService.getNews(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();		
		queryMap.put("htmlTemplateId", query_htmlTemplateId);
		queryMap.put("pubTime", query_pubTime);		
		queryMap.put("title", query_title);		

		resultList = newsService.queryNewsByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		news.setCreateTime(new Date());
		news.setCreateUser(loginUser.getUserId());	
		news.setLastUpdateTime(new Date());
		news.setLastUpdateUser(loginUser.getUserId());	
		news.setStatus("1");

		newsService.saveNews(news);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (news.getContent().length() == 0) {
			this.addActionError("** Content 不能为空 !");
		}
		if (news.getTitle().length() == 0) {
			this.addActionError("** Title 不能为空 !");
		}		

		this.setValidatorResult("/sample/NewsAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		news = newsService.getNews(id);

		return RESULT_EDIT;
	}

	public String update() {
		news.setLastUpdateTime(new Date());
		news.setLastUpdateUser(loginUser.getUserId());	

		newsService.updateNews(news);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (news.getContent().length() == 0) {
			this.addActionError("** Content 不能为空 !");
		}
		if (news.getTitle().length() == 0) {
			this.addActionError("** Title 不能为空 !");
		}
		
		this.setValidatorResult("/sample/NewsUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.newsService.delete(ids);

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNews(News news) {
		this.news = news;
	}

	public News getNews() {
		return news;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}


	public String getQuery_htmlTemplateId() {
		return this.query_htmlTemplateId;
	}

	public void setQuery_htmlTemplateId(String htmlTemplateId) {
		this.query_htmlTemplateId = htmlTemplateId;
	}

	public String getQuery_pubTime() {
		return this.query_pubTime;
	}

	public void setQuery_pubTime(String pubTime) {
		this.query_pubTime = pubTime;
	}

	public String getQuery_title() {
		return this.query_title;
	}

	public void setQuery_title(String title) {
		this.query_title = title;
	}	
	
	public HtmlTemplateService getHtmlTemplateService() {
		return htmlTemplateService;
	}

	public void setHtmlTemplateService(HtmlTemplateService htmlTemplateService) {
		this.htmlTemplateService = htmlTemplateService;
	}

	public List<HtmlTemplate> getHtmlTemplateList() {
		return htmlTemplateList;
	}

	public void setHtmlTemplateList(List<HtmlTemplate> htmlTemplateList) {
		this.htmlTemplateList = htmlTemplateList;
	}

}
